<?php
$this->pageTitle = 'Manage Users';
$this->breadcrumbs = array(
	'Users'=>array('index'),
	'Manage',
);
  

 
//$this->menu=array(
//	array('label'=>'Create Users', 'url'=>array('create')),
//);
//
//ShortUtil::registerScript('search', "
//$('.search-button').click(function(){
//	$('.search-form').toggle();
//	return false;
//});
//$('.search-form form').submit(function(){
//	$.fn.yiiGridView.update('users-grid', {
//		data: $(this).serialize()
//	});
//	return false;
//});
//
//");


?>
<div class="heading">
    <h1><img src="<?php echo ShortUtil::themePath() ?>/images/default.png" /> Manage Users</h1>
    
    <div class="controls"> 
        <?php
        echo CHtml::button('Create', array('class' => 'controls', 'id' => 'create-users'));
        echo CHtml::button('Delete', array('class' => 'controls', 'id' => 'delete-users'));
        ?>
    </div>
</div>

<div class="content">
    
<?php
// echo CHtml::link('Advanced Search','#',array('class'=>'search-button'));
// echo '<div class="search-form" style="display:none">';   
// $this->renderPartial('_search',array(
//	'model'=>$model,
// ));
// echo '</div><!-- search-form -->'; 
?>


<?php $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'users-grid',
	'dataProvider'=>$model->search(),
	'filter'=>$model,
        'cssFile' => ShortUtil::themePath() . '/css/gridview.css',
        'htmlOptions'=>array('class'=>'grid-view clear'),
        'afterAjaxUpdate'=>'js:function(id,data){$.bind_data()}',
	'columns'=>array(
        array(
            'class' => 'CCheckBoxColumn',
            'selectableRows' => 2,
            'id' => 'chk',
            'value' => $data->id,
        ),
		'id',
		'username',
		'password',
		'email',
		'createtime' => array(
                    'name' => 'createtime',
                    'value' => 'date("d-m-Y", $data->createtime)'
                ),
		/*
		'lastvisit',
		'superuser',
		'status',
		'data_range',
		*/
		array(
                        'class'=>'CButtonColumn',
                        'viewButtonImageUrl' => false,
                        'updateButtonImageUrl' => false,
                        'deleteButtonImageUrl' => false,
                        'template' => '[{view}] [{update}]'
                      
		),
	),
)); ?>
</div>

<script  type="text/javascript">
    $(document).ready(function() {
        $. bind_data = function(){
            $(".grid-view a.view").click(function() {
                var url = $(this).attr('href') + '?ajax=true';
                $.fancybox.open({
                    href : url,
                    type : 'iframe',
                    afterClose: function(){
                        $.fn.yiiGridView.update('users-grid')
                    }
                });
                return false;
            });
            
            $(".grid-view a.update").click(function() {
                var url = $(this).attr('href') + '?ajax=true';
                $.fancybox.open({
                    href : url,
                    type : 'iframe',
                    afterClose: function(){
                        $.fn.yiiGridView.update('users-grid')
                    }
                });
                return false;
            });
        }
        
        // bind data
        $. bind_data();
        
        $("#create-users").click(function() {
                $.fancybox.open({
                    href : '<?php echo $this->createUrl('create', array('ajax' => true)) ?>',
                    type : 'iframe',
                    afterClose: function(){
                        $.fn.yiiGridView.update('users-grid')
                    }
                });
                return false;
            });
        
        $('#delete-users').live('click', function(){
            var count=0;
            var params = 'YII_CSRF_TOKEN=<?php echo  Yii::app()->request->csrfToken; ?>';
            $("input[name='chk[]']").each(function(){
            if($(this).is(":checked")){
                count++;     
                params += '&chk[]=' + $(this).val();
            }
            });
            if(count >= 1 ){
            if(!confirm("Delete selected records"))
                return false;
            }else{
                alert("No checkbox is selected");
                return false;
            } 
            
            $.ajax({
                    type: "POST",
                    url: '<?php echo $this->createUrl('delete', array('ajax' => true)) ?>',
                    data: params,
                    success: function(data) {
                        $.fn.yiiGridView.update('users-grid');
                    } //success
                });//ajax
                return false;
        });
    });
</script>